package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.itany.book.service.IBookService;
import com.itany.mapper.BookMapper;
import com.itany.pojo.BookWithBLOBs;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class BookServiceImpl implements IBookService {

    @Autowired(required = false)
    private BookMapper mapper;

    @Autowired
    private HttpSolrClient solrClient;

    @Override
    public void addDoc() {
        PageHelper.startPage(1, 100);
        List<BookWithBLOBs> list = mapper.selectAllForSolr();
        //需要两表关联将书籍与出版社名称等数据存入solr
        // t_book.id,bookintroduce,author,price,imgurl,
        // publishdate,t_press.name 'press.name'
        try {
            for (int i = 0; i < list.size(); i++) {
                SolrInputDocument document = new SolrInputDocument();
                document.addField("id", list.get(i).getId());
                document.addField("bookintroduce", list.get(i).getBookintroduce());
                document.addField("bookname", list.get(i).getBookname());
                document.addField("author", list.get(i).getAuthor());
                document.addField("price", list.get(i).getPrice());
                document.addField("imgurl", list.get(i).getImgurl());
                document.addField("publishdate", list.get(i).getPublishdate());
                if (list.get(i).getPress() != null) {
                    document.addField("pressname", list.get(i).getPress().getName());
                }
                solrClient.add(document);
            }
             /* solrClient.deleteById("1");
              solrClient.deleteByQuery("*:*");*/
            solrClient.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                solrClient.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }


    }

    @Override
    public Map<String, Object> findDoc(Integer page, Integer rows, String keyWord) {
        Map<String, Object> rsMap = new HashMap<String, Object>();
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        // id,bookintroduce,author,price,imgurl,
        // publishdate,pressname,bookname
        try {
            //通过关键字生成查询语句
            String q = "*:*";
            if (keyWord != null && !"".equals(keyWord)) {
                q = "bookintroduce:" + keyWord
                        + " OR " + "author:" + keyWord
                        + " OR " + "bookname:" + keyWord
                        + " OR " + "pressname:" + keyWord
                ;
            }
            //声明查询请求对象
            SolrQuery query = new SolrQuery();
            query.setQuery(q);
            //是否开启高亮
            query.setHighlight(true);
            //设置高亮标签
            query.setHighlightSimplePre("<font color='red'>");
            query.setHighlightSimplePost("</font>");
            //设置参与高亮的列
            query.setParam("hl.fl", "author", "bookname", "pressname", "bookintroduce");
//
            //分页设置
            query.setStart((page - 1) * rows);
            query.setRows(rows);
            //执行查询
            QueryResponse response = solrClient.query(query);
            //获取结果集对象
            SolrDocumentList results = response.getResults();
            //获取总数据条数
            System.out.println("总数据条数" + results.getNumFound());
            //循环设置高亮数据
            for (int i = 0; i < results.size(); i++) {
                Map<String, String> vo = new HashMap<String, String>();
                SolrDocument sdoc = results.get(i);
                String id = (String) sdoc.get("id");
                vo.put("id", id);
                //获取高亮后的数据
                Map<String, List<String>> map = response.getHighlighting().get(id);
                List<String> authors = map.get("author");
                if (authors != null) {
                    vo.put("author", authors.get(0));
                } else {
                    vo.put("author", (String) sdoc.get("author"));
                }

                List<String> booknames = map.get("bookname");
                if (booknames != null) {
                    vo.put("bookname", booknames.get(0));
                } else {
                    vo.put("bookname", (String) sdoc.get("bookname"));
                }
                List<String> pressnames = map.get("pressname");
                if (pressnames != null) {
                    vo.put("pressname", pressnames.get(0));
                } else {
                    vo.put("pressname", (String) sdoc.get("pressname"));
                }
                List<String> bookintroduces = map.get("bookintroduce");
                if (bookintroduces != null) {
                    vo.put("bookintroduce", bookintroduces.get(0));
                } else {
                    vo.put("bookintroduce", (String) sdoc.get("bookintroduce"));
                }
                List<String> prices = map.get("price");
                if (prices != null) {
                    vo.put("price", prices.get(0));
                } else {
                    vo.put("price", (String) sdoc.get("price"));
                }
                List<String> imgurls = map.get("imgurl");
                if (imgurls != null) {
                    vo.put("imgurl", imgurls.get(0));
                } else {
                    vo.put("imgurl", (String) sdoc.get("imgurl"));
                }
                List<String> publishdates = map.get("publishdate");
                if (publishdates != null) {
                    vo.put("publishdate", publishdates.get(0));
                } else {
                    vo.put("publishdate", (String) sdoc.get("publishdate"));
                }

                list.add(vo);
            }


            rsMap.put("total", results.getNumFound());
            rsMap.put("rows", list);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return rsMap;
    }
}
